local PANEL = {}

AccessorFunc( PANEL, "m_fAnimSpeed", 	"AnimSpeed" )
AccessorFunc( PANEL, "Entity", 			"Entity" )
AccessorFunc( PANEL, "vCamPos", 		"CamPos" )
AccessorFunc( PANEL, "fFOV", 			"FOV" )
AccessorFunc( PANEL, "vLookatPos", 		"LookAt" )
AccessorFunc( PANEL, "aLookAngle", 		"LookAng" )
AccessorFunc( PANEL, "colAmbientLight", "AmbientLight" )
AccessorFunc( PANEL, "colColor", 		"Color" )
AccessorFunc( PANEL, "bAnimated", 		"Animated" )

PANEL.Drawing = false
PANEL.ModelName = nil
PANEL.GearTable = {}


--[[---------------------------------------------------------
   Name: Init
-----------------------------------------------------------]]
function PANEL:Init()

	self.Entity = nil
	self.LastPaint = 0
	self.DirectionalLight = {}
	
	self:SetCamPos( Vector( 50, 50, 50 ) )
	self:SetLookAt( Vector( 0, 0, 40 ) )
	self:SetFOV( 70 )
	
	self:SetText( "" )
	self:SetAnimSpeed( 0.5 )
	self:SetAnimated( false )
	
	self:SetAmbientLight( Color( 50, 50, 50 ) )
	
	self:SetDirectionalLight( BOX_TOP, Color( 255, 255, 255 ) )
	self:SetDirectionalLight( BOX_FRONT, Color( 255, 255, 255 ) )
	
	self:SetColor( Color( 255, 255, 255, 255 ) )

end

--[[---------------------------------------------------------
   Name: SetDirectionalLight
-----------------------------------------------------------]]
function PANEL:SetDirectionalLight( iDirection, color )
	self.DirectionalLight[iDirection] = color
end

--[[---------------------------------------------------------
   Name: OnSelect
-----------------------------------------------------------]]
function PANEL:SetModel( strModelName, itemtable, sequence )
	-- Note - there's no real need to delete the old 
	-- entity, it will get garbage collected, but this is nicer.
	if ( IsValid( self.Entity ) ) then
		self.Entity:Remove()
		self.Entity = nil		
	end
	
	-- Note: Not in menu dll
	if ( !ClientsideModel ) then return end
	
	self.Entity = ClientsideModel( strModelName, RENDER_GROUP_OPAQUE_ENTITY )
	self.Entity:SetNoDraw( true )
	if(self.VisibleItems != nil)then
		for k, v in pairs(self.VisibleItems)do
			v:SetParent(self.Entity)
			v:SetNoDraw( true )
		end
	end
	if ( !IsValid(self.Entity) ) then return end
	
	self.ModelName = strModelName
		if(itemtable)then
			self:EquipGear(self.Entity, itemtable)
		end
	
	local iSeq = self.Entity:LookupSequence( "idle_all_01" );
	
	self.Entity:SetEyeTarget(self.Entity:GetAngles():Forward()*200)
	if(sequence != nil)then
		local iSeq = self.Entity:LookupSequence( sequence );	
	end
	
	self.Entity:ResetSequence( iSeq )
	
	end
	
	

--[[---------------------------------------------------------
   Name: OnMousePressed
-----------------------------------------------------------]]
function PANEL:Paint()
	if(!self.Entity)then
		return true
	end
	self.Entity:SetNoDraw( true )
	if(self.VisibleItems != nil)then
		for k, v in pairs(self.VisibleItems)do
			v:SetNoDraw( true )
		end
	end
	self.Drawing = true
	if ( !IsValid( self.Entity ) ) then return end
	
	local x, y = self:LocalToScreen( 0, 0 )
	
	self:LayoutEntity( self.Entity )
	
	self.Entity:SetColor(self.colColor)
	
	local ang = self.aLookAngle
	if ( !ang ) then
		ang = (self.vLookatPos-self.vCamPos):Angle()
	end
	
	local w, h = self:GetSize()
	cam.Start3D( self.vCamPos, ang, self.fFOV, x, y, w, h, 5, 4096 )
	cam.IgnoreZ( true )
	
	render.SuppressEngineLighting( true )
	render.SetLightingOrigin( self.Entity:GetPos() )
	render.ResetModelLighting( self.colAmbientLight.r/255, self.colAmbientLight.g/255, self.colAmbientLight.b/255 )
	render.SetColorModulation( self.colColor.r/255, self.colColor.g/255, self.colColor.b/255 )
	render.SetBlend( self.colColor.a/255 )
	
	for i=0, 6 do
		local col = self.DirectionalLight[ i ]
		if ( col ) then
			render.SetModelLighting( i, col.r/255, col.g/255, col.b/255 )
		end
	end
	self.Entity:DrawModel()
	
	if(self.VisibleItems != nil)then
		for k, v in pairs(self.VisibleItems) do
			v:DrawModel()
		end
	end
	
	render.SuppressEngineLighting( false )
	cam.IgnoreZ( false )
	cam.End3D()
	
	self.LastPaint = RealTime()
	self.Entity:SetNoDraw( true )
	self.Drawing = false
end

function PANEL:Think()
end
--[[---------------------------------------------------------
   Name: RunAnimation
-----------------------------------------------------------]]
function PANEL:RunAnimation()
	self.Entity:FrameAdvance( (RealTime()-self.LastPaint) * self.m_fAnimSpeed )	
end

--[[---------------------------------------------------------
   Name: RunAnimation
-----------------------------------------------------------]]
function PANEL:StartScene( name )
	
	if ( IsValid( self.Scene ) ) then
		self.Scene:Remove()
	end
	
	self.Scene = ClientsideScene( name, self.Entity )
	
end



--[[---------------------------------------------------------
   Name: LayoutEntity
-----------------------------------------------------------]]
function PANEL:LayoutEntity( Entity )

end

--[[---------------------------------------------------------
   Name: GenerateExample
-----------------------------------------------------------]]
function PANEL:GenerateExample( ClassName, PropertySheet, Width, Height )

	local ctrl = vgui.Create( ClassName )
		ctrl:SetSize( 300, 300 )
		ctrl:SetModel( "models/error.mdl" )
		
	PropertySheet:AddSheet( ClassName, ctrl, nil, true, true )

end

function PANEL:EquipGear(ent, geartable)
	for l, b in pairs(geartable)do
		for k, v in pairs(ITEM_LIST)do
			if( v[9] == b )then
				print(ent:GetModel())
				self:DisplayCSItem(b, ent)
			end
		end
	end
end

function PANEL:DisplayCSItem(id, ent)

	print("Placed item"..id.."")
	
	local item = ClientsideModel( self.ModelName, RENDER_GROUP_OPAQUE_ENTITY )
	item:SetPos(ent:GetPos())
	item:SetParent(ent)
	item:SetCollisionGroup(COLLISION_GROUP_DEBRIS)
	item:SetModel(ITEM_LIST[id][5])
	item:SetSkin(ITEM_LIST[id][6])
	item:AddEffects(EF_BONEMERGE)
	
	if(!self.VisibleItems)then
		self.VisibleItems = {}
	end
	
	self.VisibleItems[ITEM_LIST[id][2]] = item
	
	self.Entity:SetNoDraw( true )
	if(self.VisibleItems != nil)then
		for k, v in pairs(self.VisibleItems)do
			v:SetNoDraw( true )
		end
	end
end

function PANEL:Rotate90Left()
	local LeftAngle = self.Entity:GetAngles().y
	LeftAngle = math.ApproachAngle( LeftAngle, LeftAngle-90, 90 ) 
	self.Entity:SetAngles(Angle(self.Entity:GetAngles().p, LeftAngle, self.Entity:GetAngles().r))
end

function PANEL:Rotate90Right()
	local RightAngle = self.Entity:GetAngles().y
	RightAngle = math.ApproachAngle( RightAngle, RightAngle+90, 90 ) 
	self.Entity:SetAngles(Angle(self.Entity:GetAngles().p, RightAngle, self.Entity:GetAngles().r))
end





derma.DefineControl( "DModelPanelGear", "A panel containing a model (wearing gear)", PANEL, "DButton" )